package com.example.exception;


import com.common.result.Result;
import com.example.exception.createException.DeletionNotAllowedException;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
@Order(1)
@RestControllerAdvice
public class SqlException {
    //添加员工异常处理
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result exceptionHandler(SQLIntegrityConstraintViolationException e) {
        e.printStackTrace();
        if (e.getMessage().contains("Duplicate entry")){
            //Duplicate entry '111' for key 'employee.idx_username'
            String[] s = e.getMessage().split(" ");
            String msg = s[2] + "已存在";
            return Result.error(msg);
        }
        return Result.error("未知错误");
    }

    //IllegalArgumentException  JWT String argument cannot be null or empty.
    @ExceptionHandler(IllegalArgumentException.class)
    public Result IllegalArgumentExceptionExceptionHandler(IllegalArgumentException e) {
        e.printStackTrace();
        return Result.error(e.getMessage());
    }

    //ExpiredJwtException
    @ExceptionHandler(ExpiredJwtException.class)
    public Result ExpiredJwtExceptionExceptionHandler(ExpiredJwtException e) {
        e.printStackTrace();
        return Result.error("token已过期");
    }


    @ExceptionHandler(DeletionNotAllowedException.class)
    public Result DeletionNotAllowedException(DeletionNotAllowedException e) {
        e.printStackTrace();
        return Result.error(e.getMessage());
    }



}
